package org.qydjk.console.service.impl;

import org.qydjk.common.pojo.company.Company;
import org.qydjk.common.pojo.Dept;
import org.qydjk.common.pojo.Employee;
import org.qydjk.common.pojo.company.InviteOffer;
import org.qydjk.common.pojo.company.InviteRecord;
import org.qydjk.common.pojo.user.User;
import org.qydjk.common.pojo.company.CompanyProduct;
import org.qydjk.common.service.impl.BaseService;
import org.qydjk.common.util.BeanUtil;
import org.qydjk.console.service.ICompanyService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CompanyService extends BaseService implements ICompanyService {

    /**
     * 删除企业
     * @param id
     * @throws Exception
     */
    public void delCompany(Integer id) throws Exception {
        List<Dept> deptlist = baseDao.findBeanBySQL("select * from qy_dept where company_id="+id,Dept.class);
        if(deptlist.size()>0){
            for(Dept dept:deptlist){
                delDept(dept.getId());
            }
        }
        Company company = baseDao.get(Company.class,id);
        baseDao.delete(company);
    }

    /**
     * 删除部门
     * @param id
     * @throws Exception
     */
    public void delDept(Integer id) throws Exception {
        List<Employee> user = baseDao.findBeanBySQL("select * from qy_employee where dept_id="+id,Employee.class);
        if(user.size()>0){
            baseDao.deleteAll(user);
        }
        Dept dept = baseDao.get(Dept.class,id);
        baseDao.delete(dept);
    }

    public String addEmpany(Employee employee) throws Exception {
        //根据手机号和身份证判断唯一
        int empcount = baseDao.getRecordCountSQL("select count(id) from qy_employee where phone = '"+employee.getPhone()+"' or card = '"+employee.getCard()+"'");
        if(empcount>0){
            return employee.getName();
        }
        int usercount = baseDao.getRecordCountSQL("select count(id) from qy_user where phone = '"+employee.getPhone()+"'");
        //如果用户表没有则新建用户
        if(usercount==0){
            try {
                User user = new User();
                user.setUsername(employee.getPhone());
                user.setPhone(employee.getPhone());
                user.setTrueName(employee.getName());
                user.setNickname(employee.getPhone());
                user.setSex(employee.getSex());
                user.setEmail(employee.getEmail());
                //默认一般用户，状态可用
                user.setCreateTime(new Date());
                user.setRegTime(new Date());
                baseDao.saveOrUpdate(user);
            } catch (Exception e) {
                return employee.getName();
            }
        }
        baseDao.saveOrUpdate(employee);
        return "OK";
    }

    public String  addEmpanyByExcel(Employee employee, List<Map<String, Object>> list) throws Exception {
        for(Map map:list){
            Employee emp = new Employee();
            BeanUtil.copyProperties(employee,emp);
            emp.setId(null);
            emp.setCard(map.get("col_0").toString());
            emp.setName(map.get("col_1").toString());
            emp.setSex(Integer.parseInt(map.get("col_2").toString()));
            emp.setPhone(map.get("col_3").toString());
            emp.setEmail(map.get("col_4").toString());
            String res = addEmpany(emp);
            if(!"OK".equals(res))return res;
        }
        return "OK";
    }

    /**
     * 发布招标
     * @param inviteRecord
     */
    public void  putPublish(InviteRecord inviteRecord) throws Exception {
            //先保存招标信息，获取招标ID
            baseDao.saveOrUpdate(inviteRecord);
            int inviteId = inviteRecord.getId();
            //复制单项分类
            int productId = inviteRecord.getProductId();
            String addCategorysql = "insert into qy_invite_single_category (single_category_id,name,create_time,update_time,invite)\n" +
                    "select c.single_project_category_id,p.name,c.create_time,c.update_time,"+inviteId+
                    " from qy_company_product_category c\n" +
                    "left join qy_single_project_category p on c.single_project_category_id=p.id\n" +
                    "where c.company_product_id="+inviteRecord.getProductId();
            baseDao.excuteSQL(addCategorysql);
            //复制单项
            String addSinglesql = "insert into qy_invite_single (single_id,single_project_category_id,name,price,des,significance,create_time,ord,inviteId)\n" +
                    "select c.single_project_id,c.single_project_category_id,s.name,s.price,s.des,s.significance,c.create_time,s.ord,"+inviteId+
                    " from qy_company_product_single_project c \n" +
                    "left join qy_single_project s on s.id=c.single_project_id \n" +
                    "where c.company_product_id = "+inviteRecord.getProductId();
            baseDao.excuteSQL(addSinglesql);
            //更新企业产品发布状态
            CompanyProduct product = baseDao.get(CompanyProduct.class,inviteRecord.getProductId());
            product.setIssue(1);
            baseDao.saveOrUpdate(product);
    }
    /*插销发布*/
    public void updateInvite(Integer invited) throws Exception {
        //修改招标主表状态
        InviteRecord inviteRecord = baseDao.get(InviteRecord.class,invited);
        inviteRecord.setCancelTime(new Date());
        inviteRecord.setStatus(1);
        baseDao.saveOrUpdate(inviteRecord);
    }
    /*确认报价*/
    public void updateOffer(Integer offerId) throws Exception {
        InviteOffer offer = baseDao.get(InviteOffer.class,offerId);
        //将所有除了当前的报价置为拒绝
        int inviteId = offer.getInviteId();
        String updateSql = "UPDATE qy_invite_offer SET status=3 WHERE invite_id="+inviteId;
        baseDao.excuteSQL(updateSql);
        offer.setStatus(2);
        baseDao.saveOrUpdate(offer);
        //修改报价主表状态
        InviteRecord record = baseDao.get(InviteRecord.class,inviteId);
        record.setStatus(2);
        baseDao.saveOrUpdate(record);
    }
}
